export let ONLINE_SERVER_ADDRESS_BASE = 'http://120.79.137.61:8088';
// export let ONLINE_SERVER_ADDRESS_BASE = 'http://47.106.103.154:8088';
import { pathToBase64, base64ToPath } from './index';

async function myUploadFile(token, ofile) {
	let imgId = -1
	// const a = await pathToBase64(ofile);
	// console.log("文件pathToBase64为：",a)
	// console.log("[info]","正在上传文件" + a + "token:" + token)
	const res = await uni.uploadFile({
		url: `${ONLINE_SERVER_ADDRESS_BASE}/file/upload`, //仅为示例，非真实的接口地址
		filePath: ofile,
		name: 'file',
		formData: {
			'user': 'test'
		},
		method:'POST',
		header: {
			// 'content-type': 'application/json',
			'Authorization': token,
			// 'content-type': 'multipart/form-data',
		},
	});	
	const [err,{data}] = res;
	console.log("文件res为：",JSON.parse(data))
	let result = JSON.parse(data)
	imgId = result.data
    return new Promise((resolve, reject) => {
            resolve(imgId)
    })
}
async function myDownloadFile(token, fileId) {
	let imgId = -1
	token = uni.getStorageSync('token')
	console.log("[info]","正在下载文件：" + fileId + "token:" + token)
	const res = await uni.downloadFile({
		url: `${ONLINE_SERVER_ADDRESS_BASE}/file/download` + fileId, //仅为示例，非真实的接口地址
		header: {
			// "Content-Type": "application/x-www-form-urlencoded",
			'Authorization': token,
		},
	});	
	console.log("文件res为：",res[1])
	// let result = JSON.parse(data)
	// imgId = result.data
    return new Promise((resolve, reject) => {
            resolve(res[1].tempFilePath)
    })
}

async function syncGetOrder(url,flag) {
	let token = uni.getStorageSync('token')
	console.log("[info]","异步获取工单：" + "token:" + token)
	if(flag){
		const res = await uni.request({
			url: `${ONLINE_SERVER_ADDRESS_BASE}/` + url, //仅为示例，非真实的接口地址
			method:'GET',
		});	
		console.log("异步结果：",res[1])
		return new Promise((resolve, reject) => {
		        resolve(res[1])
		})
	}else{
		const res = await uni.request({
			url: `${ONLINE_SERVER_ADDRESS_BASE}/` + url, //仅为示例，非真实的接口地址
			header: {
				'Authorization': token,
			},
			method:'GET',
		});	
		console.log("异步结果：",res[1])
		return new Promise((resolve, reject) => {
		        resolve(res[1])
		})
	}

}

async function syncGetOrderList(url, state, pageNo, pageSize) {
	let token = uni.getStorageSync('token')
	console.log("[info]","异步获取工单列表：" + "token:" + token)
	const res = await uni.request({
		url: `${ONLINE_SERVER_ADDRESS_BASE}/` + url, //仅为示例，非真实的接口地址
		header: {
			'Authorization': token,
		},
		method:'GET',
		data:{
			pageNo:pageNo,
			pageSize:pageSize,
			status: state
		},
	});	
	console.log("异步结果：",res[1])
    return new Promise((resolve, reject) => {
            resolve(res[1])
    })
}

async function syncInvoiceableList(url, state, pageNo, pageSize) {
	let token = uni.getStorageSync('token')
	console.log("[info]","异步获取工单列表：" + "token:" + token)
	const res = await uni.request({
		url: `${ONLINE_SERVER_ADDRESS_BASE}/` + url, //仅为示例，非真实的接口地址
		header: {
			'Authorization': token,
		},
		method:'POST',
		data:{
			// meetingRoomReserveStatus:2,
			// overtimeAcStatus:0,
			// paidServiceStatus:0,
			// repairStatus:8,
			meetingRoomReserveStatus:state[0],
			overtimeAcStatus:state[1],
			paidServiceStatus:state[2],
			repairStatus:state[3],
			paymentNoticeStatus:state[4],
		},
	});	
	console.log("异步结果：",res[1])
    return new Promise((resolve, reject) => {
            resolve(res[1])
    })
}

async function syncGetGoodsList(url, state, pageNo, pageSize, area) {
	let token = uni.getStorageSync('token')
	console.log("[info]","异步获取工单列表：" + "token:" + token)
	const res = await uni.request({
		url: `${ONLINE_SERVER_ADDRESS_BASE}/` + url, //仅为示例，非真实的接口地址
		header: {
			'Authorization': token,
		},
		method:'GET',
		data:{
			area:area,
			pageNo:pageNo,
			pageSize:pageSize,
			status: state
		},
	});	
	console.log("异步结果：",res[1])
    return new Promise((resolve, reject) => {
            resolve(res[1])
    })
}

async function syncGoodsInfo(url) {
	let token = uni.getStorageSync('token')
	console.log("[info]","异步获取工单列表：" + "token:" + token)
	const res = await uni.request({
		url: `${ONLINE_SERVER_ADDRESS_BASE}/` + url, //仅为示例，非真实的接口地址
		header: {
			'Authorization': token,
		},
		method:'GET',
	});	
	console.log("异步结果：",res[1])
    return new Promise((resolve, reject) => {
            resolve(res[1])
    })
}

async function syncPaidServiceList(pageNo, pageSize) {
	let token = uni.getStorageSync('token')
	console.log("[info]","异步获取收费服务列表：" + "token:" + token)
	const res = await uni.request({
		url: `${ONLINE_SERVER_ADDRESS_BASE}/` + "paid-service/list", //仅为示例，非真实的接口地址
		header: {
			'Authorization': token,
		},
		method:'GET',
		data:{
			pageNo:pageNo,
			pageSize:pageSize,
			title:"",
		},
	});	
	console.log("收费服务：",res[1])
    return new Promise((resolve, reject) => {
            resolve(res[1])
    })
}

async function syncGetSysUsrInfo(url) {
	let token = uni.getStorageSync('token')
	// console.log("[info]","异步获取工单：" + "token:" + token)
	const res = await uni.request({
		url: `${ONLINE_SERVER_ADDRESS_BASE}/` + url, //仅为示例，非真实的接口地址
		header: {
			'Authorization': token,
		},
		method:'GET',
	});	
	console.log("异步结果：",res[1])
    return new Promise((resolve, reject) => {
            resolve(res[1])
    })
}

async function syncRuleInfo(type) {
	let token = uni.getStorageSync('token')
	console.log("[info]","异步获取工单列表：" + "token:" + token)
	const res = await uni.request({
		url: `${ONLINE_SERVER_ADDRESS_BASE}/business-rule/show/type/` + type, //仅为示例，非真实的接口地址
		header: {
			'Authorization': token,
		},
		method:'GET',
	});	
	console.log("异步结果：",res[1])
    return new Promise((resolve, reject) => {
            resolve(res[1])
    })
}


async function syncPriceInfo(servieType,areaType,buildId,cardType) {
	let token = uni.getStorageSync('token')
	console.log("[info]","异步获取单价列表：" + "token:" + token)
	let data = {}
	if(areaType == 0){  //如果是全区域类，则data传feeObjectType参数
		data.feeObjectType = areaType
	}else{              //如果是楼盘类，则data传feeObjectType 及 buildId参数
		data.feeObjectType = areaType
		data.buildId = buildId
	}
	// if(servieType == 3){  //如果是停车位，则data多传 cardType 参数
	// 	data.cardType = cardType
	// }
	const res = await uni.request({
		url: `${ONLINE_SERVER_ADDRESS_BASE}/fee-setting/show/type/` + servieType, 
		header: {
			'Authorization': token,
		},
		data:data,
		method:'POST',
	});	
	console.log("异步获取单价：",res)
    return new Promise((resolve, reject) => {
            resolve(res[1])
    })
}

async function syncGetBannerList(bannerType) {
	let token = uni.getStorageSync('token')
	console.log("[info]","异步获取banner列表：" + "token:" + token)
	const res = await uni.request({
		url: `${ONLINE_SERVER_ADDRESS_BASE}/` + "banner/list", //仅为示例，非真实的接口地址
		header: {
			'Authorization': token,
		},
		method:'GET',
		data:{
			type:bannerType
		},
	});	
	console.log("banner：",res[1])
    return new Promise((resolve, reject) => {
            resolve(res[1])
    })
}

export function uniRedirect(url, cb) {
	// console.log('redirected');
	uni.redirectTo({
		url: '/pages' + url,
		success: (res) => {
			cb && cb();
		},
		fail: function(res) {
			// console.log(res)
		},
	});
}
export {
	myUploadFile,
	myDownloadFile,
	syncGetOrder,
	syncGetOrderList,
	syncGetGoodsList,
	syncGoodsInfo,
	syncGetSysUsrInfo,
	syncPaidServiceList,
	syncRuleInfo,
	syncPriceInfo,
	syncInvoiceableList,
	syncGetBannerList,
}